var util = require('../../utils/util.js');
var Data = require("../../utils/data.js");
var api = require('../../config/api.js');
var app = getApp();

Page({
  data: {
    city: "成都",
    date: '',
    tomorrow: '',
    dates: 1,
    hotelName: '',
    level: '',
    price: 0,
    selectStar: '',
    selectStarValue: '不限',
    hotelList: [],
    pageSize:6,
    pageNum:1,
    pageNum_other:0,
    hotelList_other:[],
    noMore_other:false
  },
  getData: function () {
    let that = this;
    var data = {
      regionName:that.data.city,
      pageSize:that.data.pageSize,
      pageNum:that.data.pageNum
    }
    util.request(api.HotelIndex, data).then(function (res) {
      if (res.code === '200') {
        that.setData({
          hotelList: res.datas.hotelList,
          totalCount: res.datas.totalCount
        });
      }
    });
  },
  onShow: function () {
    // 页面显示
    var startDate = wx.getStorageSync("startDate");
    var endDate = wx.getStorageSync("endDate");
    console.log("初始时间>>>" + startDate + endDate);

    if (!startDate || startDate == '' || !endDate || endDate == '') {
      //初始化当前时间
      // wx.clearStorageSync();
      startDate = Data.formatDate(new Date(), "yyyy-MM-dd");
      wx.setStorageSync("startDate", startDate);
      var tomorrow1 = new Date();
      tomorrow1.setDate((new Date()).getDate() + 1);
      var tomorrow = Data.formatDate(new Date(tomorrow1), "yyyy-MM-dd");
      endDate = tomorrow;
      wx.setStorageSync("endDate", tomorrow);
      console.log("如果为空，计算后时间>>>" + startDate + endDate);
    }

    var aDate, oDate1, oDate2, iDays
    aDate = endDate.split("-")
    console.log("aDate>>>:length-" + aDate.length + ",0-" + aDate[0] + ",1-" + aDate[1] + ",2-" + aDate[2])
	var tempDate = new Date();
	oDate1 = new Date(tempDate.getFullYear(),tempDate.getMonth(),tempDate.getDate());
    oDate1.setFullYear(aDate[0]);
    oDate1.setMonth(aDate[1] - 1);
    oDate1.setDate(aDate[2]);

    aDate = startDate.split("-")
    oDate2 = new Date(tempDate.getFullYear(),tempDate.getMonth(),tempDate.getDate());
    oDate2.setFullYear(aDate[0]);
    oDate2.setMonth(aDate[1] - 1);
    oDate2.setDate(aDate[2]);
    console.log("oDate2》》》", oDate2)
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)    //把相差的毫秒数转换为天数  
    console.log("iDays》》》", iDays);
    this.setData({
      date: startDate,
      tomorrow: endDate,
      dates: iDays
    });
    this.getData();
    app.getShareInfo(this);
  },
  //事件处理函数
  bindViewTap: function () {
    var that = this;
    var startDate = that.data.date;
    var endDate = that.data.tomorrow;
    wx.navigateTo({
      url: '../calender/index?startDate=' + startDate + "&endDate=" + endDate
    })
  },
  selectCity: function () {
    wx.navigateTo({
      url: ''
    })
  },
  inputHotelName: function (e) {
    this.setData({
      hotelName: e.detail.value
    })
  },
  selectStarLevel: function (e) {
    this.setData({
      selectStar: e.currentTarget.dataset.type,
      selectStarValue: e.currentTarget.dataset.detail
    })
  },
  // sliderChange: function (e) {
  //   this.setData({
  //     price: (e.detail.value === 600 ? '不限' : e.detail.value)
  //   })
  // },
  complete: function () {

    this.hideModal();
  },
  
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  
  search:function(){
    var that = this;
    var city = that.data.city;
    var keyWord = that.data.hotelName;
    var startDay = that.data.date;
    var endDay = that.data.tomorrow;
    var selectStar = that.data.selectStar;
    console.log(city);
    var url = '/pages/hotelList/hotelsearch?city=' + city +
      "&startDay=" + startDay +
      "&endDay=" + endDay + "&level=" + selectStar +
      "&keyWord="+keyWord;
    wx.navigateTo({
      url: url,
    })
  },
   selectCity: function () {
    wx.navigateTo({
      url: '/pages/city/index'
    })
  },
   inputKeyWord:function(e){
     this.setData({
       keyWord:e.detail.value
     })
   }, onLoad: function (options) {
     // 页面初始化 options为页面跳转所带来的参数
     app.init(options, this);

   },
   fnGetMore:function(){
     var that = this;
     var isNoMore = that.data.noMore;
     if(isNoMore){
      //如果没有本地数据了，查询全国数据
       that.fnGetMoreDataOther();
     }else{
       //如果还有本地数据，则查询本地数据
       that.fnGetMoreData();
     }
   },
   //获取更多本地数据
   fnGetMoreData:function(){
     var that = this;
     var data = {
       regionName: that.data.city,
       pageSize: that.data.pageSize,
       pageNum: that.data.pageNum + 1
     }
     wx.showLoading({
       title: '加载中...',
     })
     util.request(api.HotelMoreRecommend, data).then(function (res) {
       wx.hideLoading();
       if (res.code === '200') {
         if (res.datas && res.datas.length == that.data.pageSize) {
           //数据够显示一页
           that.setData({
             hotelList: that.data.hotelList.concat(res.datas),
             pageNum: that.data.pageNum + 1,
             noMore: false
           })
         } else if (res.datas && res.datas.length > 0) {
           //数据不够够显示一页
           that.setData({
             hotelList: that.data.hotelList.concat(res.datas),
             pageNum: that.data.pageNum + 1,
             noMore: true
           })
         } else {
           wx.showToast({
             title: '没有更多了',
             image: "/static/images/icon_error.png",
             duration: 2000
           })
           that.setData({
             noMore: true
           })
         }
       }
     });
   },
   //获取更多本地数据
   fnGetMoreDataOther: function () {
     var that = this;
     var data = {
       regionName: that.data.city,
       pageSize: that.data.pageSize,
       pageNum: that.data.pageNum_other + 1,
       isOtherCity:1
     }
     wx.showLoading({
       title: '加载中...',
     })
     util.request(api.HotelMoreRecommend, data).then(function (res) {
       wx.hideLoading();
       if (res.code === '200') {
         if (res.datas && res.datas.length == that.data.pageSize) {
           //数据够显示一页
           that.setData({
             hotelList_other: that.data.hotelList_other.concat(res.datas),
             pageNum_other: that.data.pageNum_other + 1,
             noMore_other: false
           })
         } else if (res.datas && res.datas.length > 0) {
           //数据不够够显示一页
           that.setData({
             hotelList_other: that.data.hotelList_other.concat(res.datas),
             pageNum_other: that.data.pageNum_other + 1,
             noMore_other: true
           })
         } else {
           wx.showToast({
             title: '没有更多了',
             image: "/static/images/icon_error.png",
             duration: 2000
           })
           that.setData({
             noMore_other: true
           })
         }
       }
     });
   },
   fnShare: function (e) {
     var that = this;
     var path = "";
     var type = e.currentTarget.dataset.type;
     var goodsId = e.currentTarget.dataset.id;
     var imgUrl = e.currentTarget.dataset.url;
     var goodsName = e.currentTarget.dataset.name;
     var address = e.currentTarget.dataset.address;
     var price = e.currentTarget.dataset.price;
     var marketPrice = e.currentTarget.dataset.marketprice;
     if (type == 'hotel') {
       path = "/pages/hotelDetail/hoteldetail";
     } else if (type == "scene") {
       path = "/pages/sceneDetail/sceninfo";
     } else {
       path = "/pages/tourDetail/travelline"
     }
     app.fnShareGoods(path, goodsId, imgUrl, goodsName, "hotel", address, price, marketPrice);
   }
})